using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BlackHoleController : MonoBehaviour
{
    [SerializeField] private GameObject hotKeyPrefab;
    private float maxSize = 15;
    private float expandSpeed = 4f;
    private float shrinkSpeed = 4f;
    private bool canExpand = true;
    private bool canShrink;
    private List<KeyCode> keys;
    private List<GameObject> keyObjs = new List<GameObject>();
    private List<Transform> targets;
    private int attackAmounts = 4;
    private bool canAttack;
    private bool canCreateHotKeys = true;
    private float attackCoolDown = 0.3f;
    private float attackTimer;
    private float blackHoleTimer;


    // Update is called once per frame
    void Update()
    {
        attackTimer -= Time.deltaTime;
        blackHoleTimer -= Time.deltaTime;

        if (blackHoleTimer < 0)
        {
            blackHoleTimer = Mathf.Infinity;
            if(targets.Count == 0)
            {
                FinishBlackHole();
            }
            else
            {
                ReleaseAttacks();
            }
        }

        if (Input.GetMouseButtonDown(0))
        {
            ReleaseAttacks();
        }


        if (attackTimer < 0 && canAttack && attackAmounts > 0)
        {
            attackTimer = attackCoolDown;
            int isright = Random.Range(0, 2);
            Transform enemy = targets[Random.Range(0, targets.Count)];
            if (SkillManager.Instance.clone.crystalInsteadOfClone)
            {
                SkillManager.Instance.crystal.CreateCrystal();
            }
            else
            {
                SkillManager.Instance.clone.CreateClone(enemy.position, isright < 1 ? Vector3.right : Vector3.left);
            }
                
            attackAmounts--;
            
            if (attackAmounts <= 0)
            {
                Invoke("FinishBlackHole", .8f);
            }
        }



        if (canExpand && !canShrink)
        {
            transform.localScale = Vector3.Lerp(transform.localScale, new Vector2(maxSize, maxSize), expandSpeed * Time.deltaTime);
        }

        if (canShrink)
        {
            transform.localScale = Vector3.Lerp(transform.localScale, new Vector2(-1, -1), shrinkSpeed * Time.deltaTime);
            if(transform.localScale.x < 0)
            {
                Destroy(gameObject);
            }
        }
    }

    private void ReleaseAttacks()
    {
        if (targets.Count == 0) return;
        DestroyHotKeys();
        canAttack = true;
        canCreateHotKeys = false;
        PlayerManager.Instance.Player.MakeTransparent(true);
    }

    private void FinishBlackHole()
    {
        canCreateHotKeys = false;
        DestroyHotKeys();
        PlayerManager.Instance.Player.ExitBlackHoleAbility();
        canShrink = true;
        canAttack = false;
        PlayerManager.Instance.Player.MakeTransparent(false);
    }

    public void SetBlackController(float _maxSize,float _expandSpeed,float _shirnkSpeed,float _attackCooldown,int _attackAmounts,float _blackHoleDuration)
    {
        maxSize = _maxSize;
        expandSpeed = _expandSpeed;
        shrinkSpeed = _shirnkSpeed;
        attackCoolDown = _attackCooldown;
        attackAmounts = _attackAmounts;
        blackHoleTimer = _blackHoleDuration;
        targets = new List<Transform>();
        keys = new List<KeyCode>();
        for (int i = 103; i < 108; i++) 
        {
            keys.Add((KeyCode)i);
        }
    }


    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.GetComponent<Enemy>() != null)
        {
            CreateHotkeys(collision);

        }
    }

    private void CreateHotkeys(Collider2D collision)
    {
        collision.GetComponent<Enemy>().FreezeTime(true);
        if (keys.Count > 0 && !collision.GetComponentInChildren<BlackHole_HotKeyController>() && canCreateHotKeys)
        {
            KeyCode hotkeynum = keys[Random.Range(0, keys.Count)];
            keys.Remove(hotkeynum);
            GameObject hotkey = Instantiate(hotKeyPrefab, collision.transform, true);
            keyObjs.Add(hotkey);
            hotkey.transform.position = collision.transform.position + new Vector3(0.2f, 2.6f, 0);
            hotkey.GetComponent<BlackHole_HotKeyController>().SetHotKey(hotkeynum, collision.transform, this);
        }
       
    }

    void DestroyHotKeys()
    {
        for (int i = 0; i < keyObjs.Count; i++) 
        {
            Destroy(keyObjs[i]);
        }
    }

    public void AddEnemyToList(Transform transform) 
    {
        if(!targets.Contains(transform))
            targets.Add(transform); 
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.GetComponent<Enemy>() != null)
        {
            collision.GetComponent<Enemy>().FreezeTime(false);
        }
    }
}
